%% PLOT DEFAULT SETS
clear all;addpath('auxiliar'); addpath('Simulated_Datasets');
load SIMULATION_ERGODIC_DE
YSIM=priceSIM.*yNSIM+ySIM;
mY=mean(YSIM);

%mKappa=fix(NKAPPA/2)+1;
%mPV=fix(NPV/2)+1;
%st=(mKappa*mPV)*NT+1;
st=25;
%st=1;
def_func_extrac=squeeze(Def_func(:,:,st));



maxy_defL=zeros(NB,1);
maxy_defH=zeros(NB,1);
maxy_defL=zeros(NB,2);
set0=def_func_extrac; %squeeze(Def_func(:,Debt_low,((mKappa*mPV)*NT+1):((mKappa*mPV)*NT+NT)));
set=set0;
set(set0>=.5)=1;
set(set0<.5)=0;

 for i=1:NB
     a=find(set(:,i) >= 1, 1, 'first');
     a1(a)=a;
     if isempty(a)
       maxy_defL(i)=BGgrid(1)-.05;
     else
         maxy_defL(i)=BGgrid(a);
     end
 end

    

  for i=1:NB
    max_def(i,1)= maxy_defL(i);
   % max_def(i,2)= maxy_defH(i)-maxy_defL(i);
  end

% PLOT REPAYMENT SETS

  max_defDE=max_def;
  mYDE=mY;
clearvars -except max_defDE mYDE st

load SP_SIMULATION_20k
YSIM=priceSIM.*yNSIM+ySIM;
mY=mean(YSIM);
def_func_extrac=squeeze(Def_func(:,:,st));



maxy_defL=zeros(NB,1);
maxy_defH=zeros(NB,1);
maxy_defL=zeros(NB,2);
set0=def_func_extrac; %squeeze(Def_func(:,Debt_low,((mKappa*mPV)*NT+1):((mKappa*mPV)*NT+NT)));
set=set0;
set(set0>=.5)=1;
set(set0<.5)=0;

 for i=1:NB
     a=find(set(:,i) >= 1, 1, 'first');
     a1(a)=a;
     if isempty(a)
       maxy_defL(i)=BGgrid(1)-.05;
     else
         maxy_defL(i)=BGgrid(a);
     end
 end

    

  for i=1:NB
    max_def(i,1)= maxy_defL(i);
   % max_def(i,2)= maxy_defH(i)-maxy_defL(i);
  end

figure
    area(bgrid1d/mY,max_def*cte_debtfactor/mY,'FaceColor',	"#0072BD",'FaceAlpha',.8,'EdgeAlpha',.01) ;hold on 
area(bgrid1d/mYDE,max_defDE*cte_debtfactor/mYDE,'FaceColor', "#D95319",'FaceAlpha',1,'EdgeAlpha',.01); hold on




      txt = 'Default Zone'; hold on
      text(0.1,.3,txt,'FontSize',14); hold on

  txt = 'Repayment Zone '; hold on
      text(0.1,.1,txt,'FontSize',14); hold on

    xlim([bgrid1d(1)/mY,bgrid1d(NB)/mY])
    ylim([BGgrid(1)*cte_debtfactor/mY,BGgrid(NBG)*cte_debtfactor/mY])
  

    ylabel("Current public debt ($L$)",'Interpreter','Latex');
    xlabel("Current private debt ($B$)",'Interpreter','Latex');

   %title(title_var)

    legend({'Planner repayment zone','Baseline repayment zone'},'Location','northwest','Box','off')
    hold off
    fig = gcf;
    exportfig(gcf,'Plots\Fig8A_RepaymentZones.eps','color','cmyk','resolution',1600)
%% PORTFOLIO
    clear all ;load events
          figure
   scatter(PvDebt_to_GDP_DE,PubDebt_to_GDP_DE,20,'d','MarkerFaceAlpha',0.9,'MarkerFaceColor',"#D95319",'MarkerEdgeColor',"#D95319");hold on %'MarkerEdgeColor',"#D95319",'Marker','o' ,'MarkerFaceAlpha',1,'MarkerEdgeAlpha',.5,"filled");hold on    
      scatter(PvDebt_to_GDP,PubDebt_to_GDP,5,'MarkerFaceAlpha',0.3,'MarkerFaceColor',"#0072BD",'MarkerEdgeColor',"#0072BD"); hold on; %,'MarkerEdgeColor',"#0072BD",'Marker','o' ,'MarkerFaceAlpha',1,'MarkerEdgeAlpha',.3,"filled");hold on    


    ylabel("Public debt ($L'$)",'Interpreter','Latex')
    xlabel("Private debt ($b'$)",'Interpreter','Latex')

   %title(title_var)
xlim([-.1 .7])
    legend({'Portfolio baseline ','Portfolio planner '},'Location','northeast','Box','off')
hold off;
exportfig(gcf,'Plots\Fig8B_Portfolio.eps','color','cmyk','resolution',1600)